<?php

class login extends Controller
{
	public function index ()
	{
		$this->session();
		new View('template',
			array(
				'title' =>  'Login - FEELS',
				'heading' => '',
				'style' => '#main { background: none; }',
				//'highlight' => 'organnouncements',
				//'currentOrg' => $name,
				'globalnav' => new BufferedView('org/globalnav'),
				'content' => BufferedView::instance('public/forms/login',
						array(
							'action' => "/login/submit",
							'hiddenInputs' => array()
						)
					)
			)
		);
	}
	
	public function submit() {
		$this->session();
		$userModel = new user();
		$user = $userModel->logIn( $_POST['username'], $_POST['password'] );
		if( count($user) === 1 ) {
			$this->session();
			$_SESSION['stud_no'] = $user[0]['stud_no'];
			$_SESSION['firstname'] = $user[0]['firstname'];
			$_SESSION['middlename'] = $user[0]['middlename'];
			$_SESSION['lastname'] = $user[0]['lastname'];
			$_SESSION['admin'] = $user[0]['admin'];
			// new View('template',
				// array(
					// 'title' =>  'Login - iTamCity',
					// 'heading' => 'Login',
					// 'highlight' => 'organnouncements',
					// 'currentOrg' => $name,
					// 'content' => 
						// 'You have been logged in, ' . ucwords(strtolower($_SESSION['firstname'])) . '. Click <a href="/my">here</a> to go to the homepage. '
						// . ($_SESSION['admin'] === '1' ? 'You are an admin.' : ''),
						
					// 'globalnav' => new BufferedView('org/globalnav')
					// ,
				// )
			// );
			/*EDITED*/
			if ($_SESSION['admin'] == 1)
			header('Location: /siteadmin');
			else if ($_SESSION['admin'] == 2)
			header('Location: /my');
			else if ($_SESSION['admin'] == 0)
			header('Location: /my');
		}
		else {
			new View('template',
				array(
					'title' =>  'Login - FEELS',
					'heading' => 'Login',
					//'highlight' => 'organnouncements',
					//'currentOrg' => $name,
					'content' => BufferedView::instance('public/forms/login', 
							array(
								'action' => "/login/submit",
								'hiddenInputs' => array()
							)
						)->getBuffer()
						. nl2br('<div style="font-size: 13px; margin-top: 15px; color: #83B224">Could not find username and password combination. 
<strong>Did you forget your password?</strong>

Please proceed to the Management Information System (MIS) room located at the
4th floor Tech building. Ask for the Portal administrator to verify your password.

Tip: Change your password immediately after retrieving. </div>
'),						
					'globalnav' => new BufferedView('org/globalnav')
				)
			);
		}
	}
}
